//提示框
function rightTipBOX(msg) {
    if (msg == undefined) {
        msg = ''
    }
    $.gritter.add({
        title: "提示",
        text: msg,
    });
}

//附件下载
function download(el) {
    var fileName;
    console.log(el);
    fileName = $(el).siblings("input").val();
    var url = "/filesUpload/download?id=" + $(el).attr("FileSysId") + (!fileName ? "" : "&name=" + fileName);
    window.open(url);
}

//模态框里下载时取不到input值用placeholder取
function download2(el) {
    var fileName;
    console.log(el);
    fileName = $(el).siblings("input").attr('placeholder');
    var url = "/filesUpload/download?id=" + $(el).attr("FileSysId") + (!fileName ? "" : "&name=" + fileName);
    window.open(url);
}

/**
 * @param el  元素
 * @param formName 附件name
 * @param fileMingZi 附件名字name
 * @param elId 上传附件元素Id
 * @format 卡 格式
 */


function uploadFile(el, formName, fileMingZi, elId = "fileupload", format) {
    var file = $(el).parent().parent().find("#" + elId);
    var fileVal = file.val();
    var fileName = fileVal.split("\\");
    if (format) {
        var fileFormat = fileName[2].split(".");
        if (fileFormat[1] != format) {
            return alert("请上传格式为PDF文件")
        }
    }
    fileName = fileName[(fileName.length - 1)];
    if (fileVal == null || fileVal == "") {
        alert("未选择任何文件");
        return;
    }
    // 判断文件大小是否大于10M
    var fileSize = $("#" + elId)[0].files[0].size / 1024 / 1024;
    var formData = new FormData();
    formData.append("files", $("#" + elId)[0].files[0]);
    if (fileSize > 150) {
        alert("上传文件大小不能大于150M");
        return;
    }
    $(".shade").fadeIn(100);
    $(".warning").fadeIn(100);
    $('.la-square-jelly-box').show();
    showShade();
    //警告框自定义内容
    $('.warning>.title>em').html('加载中……');
    $.ajax({
        url: "/filesUpload/uploadBatch",
        data: formData,
        type: "post",
        processData: false,
        contentType: false,
        xhr: function () {
            myXhr = $.ajaxSettings.xhr();
            if (myXhr.upload) { // check if upload property exists
                myXhr.upload.addEventListener('progress', function (e) {
                    var loaded = e.loaded;//已经上传大小情况
                    var tot = e.total;//附件总大小
                    var per = Math.floor(100 * loaded / tot);  //已经上传的百分比
                    // console.log('附件总大小 = ' + loaded);
                    // console.log('附件总大小 = ' + loaded);
                    $('.warning>.title>em').html(per + "%");
                }, false); // for handling the progress of the upload
            }
            return myXhr;
        },
        success: function (data) {
            var data = JSON.parse(data);
            if (data.code === "200") {
                $('.warning>.title>em').html('上传成功');
                if ($("input[name=" + formName + "]")) {
                    $($("input[name=" + formName + "]")).remove();
                    $($("input[name=" + fileMingZi + "]")).remove();
                }
                var fileupload1 = "<input type=\"hidden\" name=\"" + formName + "\" value=\"" + data.data.sysid + "\">\n"
                var formFileName = "<input type=\"hidden\" name=\"" + fileMingZi + "\" value=\"" + fileName + "\">\n"
                file.after(fileupload1);
                file.after(formFileName);
            } else {
                $('.warning>.title>em').html('上传失败');
            }
        },
        error: function (data) {
            console.log(data);
            alert("附件上传失败 ！！请联系管理员");
        }
    });
}

/**
 *  字符串拼接 select 查询内容
 * @param cls
 */

function getGenre(cls) {
    var html = "";
    $.get("/taskMain/categoryList", function (req) {
        for (var i = 0; i < req.length; i++) {
            html += "<option value=\"" + req[i].sysId + "\">" + req[i].category + "</option>"
        }
        $(cls).append(html);
    });
}

/**
 *
 * @param el
 * @param url
 */

function searchInputType(el, url) {
    var $categoryId = $(el).val();
    console.log($categoryId);
    $("#gridTable").jqGrid('setGridParam', {
        url: url,
        postData: {categoryId: $categoryId}, //发送数据
        page: 1
    }).trigger("reloadGrid"); //重新载入
}

//督查组搜索
function searchTable(url) {
   var categoryId = $(".searchType").val();//获取事项类型
   var deptId = $(".deptrespts").val();//获取承办单位
   var beginTime =  $(".beginTimes").val();//获取开始时间
   var endTime =  $(".endTimes").val();//获取结束时间
   var search =  $(".searchs").val();//搜索字段

    var data = {
        categoryId:categoryId,
        deptId:deptId,
        search:search,
        startTime:beginTime,
        endTime:endTime
    }
    $("#gridTable").jqGrid('setGridParam', {
        url: url,
        postData: data, //发送数据
        page: 1
}).trigger("reloadGrid"); //重新载入
}

//承办单位搜索
function searchUnitTable(url) {
    var beginTime =  $(".beginTimes").val();//获取开始时间
    var endTime =  $(".endTimes").val();//获取结束时间
    var search =  $(".searchs").val();//搜索字段
    var data = {
        search:search,
        startTime:beginTime,
        endTime:endTime
    }

    $("#gridTable").jqGrid('setGridParam', {
        url: url,
        postData: data, //发送数据
        page: 1
    }).trigger("reloadGrid"); //重新载入
}


/**
 * @param field 传到后端的字段
 * @param url 查询的接口
 * @constructor
 */
function confirmBtn(field, url, e) {
    var fieldVal = $(e).siblings("input").val();
    $("#gridTable").jqGrid('setGridParam', {
        url: url,
        postData: {[field]: fieldVal}, //发送数据
        page: 1
    }).trigger("reloadGrid"); //重新载入
}

/**
 *
 * @param e  input
 * 获取 点击附件的名字与sysid
 */
function copyFile(e) {
    var $fileName = $(e).val();
    var $fileVal = $(e).siblings("button").attr("FileSysId");
}

/**
 *
 * @param e         禁用元素
 * @param boole     ture,false
 */

function failure(e, boole) {
    if (boole) {
        $(e).attr('disabled', true);
    } else {
        $(e).prop('disabled', false);
    }
}

//请求承办单位数据
function requestDept() {
    $.ajax({
        url: "/taskMain/cbDeptList",
        type: "POST",
        dataType: "json", //返回数据形式为json
        contentType: 'application/json;charset=UTF-8',
        data: JSON.stringify({}),
        success: function (res) {
            console.log(res[0]);
            $("#select2-chosen-1").text("请输入或点击选择承办单位");
            var html = '\
            <option value="" selected>请输入或点击选择承办单位</option>\
            ';
            for (var i = 0; i<res.length; i++) {
                html += '\
                <option value="' + res[i].sysId + '">' + res[i].deptName + '</option>\
                ';
            }
            $("#deptIds1").html(html);
        }
    });
}


//日期选择器结束时间
jQuery(function ($) {
    $(".endTimes").datepicker({
        inline: true,
        format: "yyyy-mm-dd",
        autoclose: true,
        todayHighlight: true,
        language: 'cn'
    }).next().on(ace.click_event, function () {
        $(this).prev().focus();
    });
});

//日期选择器开始时间
jQuery(function ($) {
    $(".beginTimes").datepicker({
        inline: true,
        format: "yyyy-mm-dd",
        autoclose: true,
        todayHighlight: true,
        language: 'cn'
    }).next().on(ace.click_event, function () {
        // $(this).prev().focus();
    });
});

//重置搜索框
$(function () {
    $(".resets").click(function () {
        $(".searchType").val("");
        $(".endTimes").val("");
        $(".beginTimes").val("");
        $(".searchs").val("");
        $(".deptIds").select2("val", "");
        $(".deptrespts").val("");
    });
});

$(function () {
    $("#deptIds1").change(function () {
        $(this).siblings(".deptrespts").val($(this).val().join(","));
    });
})



